import { AdsorbType, ClassName, Events } from "../../../Constants";
import { Utils } from "../../../utils";
import Pnt from "../Pnt";
import Feature from "../../Feature"
import GridSystem from "@/GridSystem";

// 吸附功能的点,方便选取用
class AdsorbPnt extends Pnt {

    adsorbTypes = [AdsorbType.POINT, AdsorbType.GRID];

    constructor(width: number = 1.4, height?:number) {   
        super(0, 0, width, height);
        this.fillStyle = this.hoverStyle = this.focusStyle = "#107EFF";
        this.className = ClassName.ADSORBPNT;
        this.zIndex = Infinity;
        this.isStroke = false;
        this.cbCapture = false;
        this.cbSelect = false;
        if (GridSystem.isMobile) {   // pc端方可使用
            document.addEventListener(Events.TOUCHSTART, this.updatePos);
        } else {
            document.addEventListener(Events.MOUSE_MOVE, this.updatePos);
        }
    }

    updatePos = (e: any) => {
        this.position = Feature.Gls.getRelativePos(Utils.getMousePos(Feature.Gls.domElement, e));
        let offsetX = 0
        let offsetY = 0
        if (this.adsorbTypes.includes(AdsorbType.POINT)) {
            const { x, y } = Feature.Gls.getAdsorb2Point(this.position);
            if (x != 0 || y != 0) {
                offsetX = x;
                offsetY = y;
                this.position.x += offsetX;
                this.position.y += offsetY;
                this.pointArr = Utils.getRectPoints(this.position, this.size);
                return
            }
        }
        if (this.adsorbTypes.includes(AdsorbType.GRID)) {
            const { x, y } = Feature.Gls.getAdsorb2Grid(this.position)
            offsetX = x;
            offsetY = y;
            this.position.x += offsetX;
            this.position.y += offsetY;
            this.pointArr = Utils.getRectPoints(this.position, this.size);
            return
        }


    }

    destroy() {
        document.removeEventListener(Events.MOUSE_MOVE, this.updatePos);
    }
}

export default AdsorbPnt;